//@author A0086393U
package rightnow.ui;

import java.util.ArrayList;

/**
 * This class is responsible for maintaining the stack of previously input
 * commands.
 */
public class CommandStack {
	private ArrayList<String> inputs = new ArrayList<String>();
	int currentPosition = -1;
	
	/**
	 * Adds a command to the end of the command stack and sets the current index
	 * to the size of the stack (1 larger than the maximum index number).
	 * @param cmd The command string input.
	 */
	protected void commandEntered(String cmd) {
		inputs.add(cmd);
		currentPosition = inputs.size();
	}
	
	/**
	 * Gets the command displayed before the currently displayed one and
	 * decrements the current index accordingly.
	 * @return Command entered before the current index.
	 */
	protected String getBackwards() {
		if (currentPosition - 1 >= 0) {
			// There is a command entered before the currently displayed one.
			return inputs.get(--currentPosition);
		} else {
			return null;
		}
	}
	
	/**
	 * Gets the command displayed after the currently displayed one and 
	 * increments the current index accordingly.
	 * @return Command entered after the current index.
	 */
	protected String getForward() {
		if (currentPosition + 1 < inputs.size()) {
			// There is a command entered after the currently displayed one
			return inputs.get(++currentPosition);
		} else {
			return null;
		}
	}
}
